钉钉获取打卡数据的操作类

pom.xml

1
2
3
4
5
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>2.0.0</version>
</dependency>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
 


/**
* 钉钉类
*
*/
@Component
public class DingdingUtil {

// 应用的appKey
private static String appKey = "";
// 应用的appSecret
private static String appSecret = "";
// access_token url
private static String accessTokenUrl = "https://oapi.dingtalk.com/gettoken";
// 考勤记录 url
private static String attendanceUrl = "https://oapi.dingtalk.com/attendance/list";

// 部门 url
private static String departmentUrl = "https://oapi.dingtalk.com/department/list";
// 员工 url
private static String userUrl = "https://oapi.dingtalk.com/user/listbypage";

@Autowired
private RedisUtils redisUtils;

/**
* 通过调用接口获取access_token
* @return String
* 2022/03/08 09:30
*/
public String getAccessToken() {
String token = (String) redisUtils.get(CacheKey.DINGDING_TOKEN);
if (ToolUtil.isEmpty(token)) {
DingTalkClient client = new DefaultDingTalkClient(accessTokenUrl);
OapiGettokenRequest request = new OapiGettokenRequest();
request.setAppkey(appKey);
request.setAppsecret(appSecret);
request.setHttpMethod("GET");
OapiGettokenResponse response = null;
try {
response = client.execute(request);
} catch (ApiException e) {
e.printStackTrace();
}
token = response.getAccessToken();
if (ToolUtil.isNotEmpty(token)) {
redisUtils.set(CacheKey.DINGDING_TOKEN, token,7200);
}
}
return token;
// DingTalkClient client = new DefaultDingTalkClient(accessTokenUrl);
// OapiGettokenRequest request = new OapiGettokenRequest();
// request.setAppkey(appKey);
// request.setAppsecret(appSecret);
// request.setHttpMethod("GET");
// OapiGettokenResponse response = null;
// try {
// response = client.execute(request);
// } catch (ApiException e) {
// e.printStackTrace();
// }
// return response.getAccessToken();
}

/**
* 获取 部门信息,
*/
public OapiDepartmentListResponse getDepartmentList(){
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
OapiDepartmentListRequest req = new OapiDepartmentListRequest();
req.setFetchChild(true);
//req.setId("1");
req.setId("529205900");
req.setHttpMethod("GET");
String token = getAccessToken();
OapiDepartmentListResponse rsp = null;
try {
rsp = client.execute(req, token);
} catch (ApiException e) {
e.printStackTrace();
}
return rsp;

}



/**
* 获取 人员信息,
* @return OapiAttendanceListResponse
*/
public OapiUserSimplelistResponse getEmpList(Long deptid,Long offset,Long size){
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/listbypage");
OapiUserSimplelistRequest req = new OapiUserSimplelistRequest();
req.setDepartmentId(deptid);
req.setOffset(offset);
req.setSize(size);
req.setHttpMethod("GET");
String token = getAccessToken();
OapiUserSimplelistResponse rsp = null;
try {
rsp = client.execute(req, token);
} catch (ApiException e) {
e.printStackTrace();
}
return rsp;
}



/**
* 获取 人员信息,化简版,只传部门id
* @return OapiAttendanceListResponse
* 2022-04-14
*/
public OapiUserSimplelistResponse getEmpSimpleList(Long deptid){
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/simplelist");
OapiUserSimplelistRequest req = new OapiUserSimplelistRequest();
req.setDepartmentId(deptid);
req.setHttpMethod("GET");
String token = getAccessToken();
OapiUserSimplelistResponse rsp = null;
try {
rsp = client.execute(req, token);
} catch (ApiException e) {
e.printStackTrace();
}
return rsp;
}


// 7100704, 工作时长
// 7100705, 迟到次数
// 7100706, 迟到时长
// 7100710, 早退次数
// 7100711, 早退时长
// 7100724, 第一次打卡时间
// 7100726, 第二次打卡时间
// 7100728, 第三次打卡时间
// 7100730, 第四次打卡时间
// 7100732, 第五次打卡时间
// 7100734, 第六次打卡时间
// 7100736, 关联表单日期及详情
// 7100701, 出勤班次
// 7100715, 出差时长
// 7100716, 外出时长
// 7100723, 班次
private static final String COLS="7100704,7100705,7100706,7100710,7100711,7100724,7100726,7100728,7100730,7100732,7100734,7100736,7100701,7100715,7100716,7100723";



/**
* 获取 人员考勤信息
* @param userId
* @param from
* @param to
* @return OapiAttendanceListResponse
*/

public OapiAttendanceGetcolumnvalResponse getColumnvalList(String userId, String from, String to) {

// 获取access_token
String access_token = getAccessToken();
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getcolumnval");
OapiAttendanceGetcolumnvalRequest req = new OapiAttendanceGetcolumnvalRequest();
req.setUserid(userId);
req.setColumnIdList(COLS);
req.setFromDate(StringUtils.parseDateTime(from +" 00:00:00"));
req.setToDate(StringUtils.parseDateTime(to + " 23:59:59"));
OapiAttendanceGetcolumnvalResponse response = null;
try {
response = client.execute(req, access_token);

} catch (ApiException e) {
e.printStackTrace();
}
return response;
}

/**
* 判断是否有打卡结果
* @return OapiAttendanceListResponse
* 2022/03/08 09:45
*/
public OapiAttendanceListResponse getAttendanceList(String from, String to, String userId) {
// 获取access_token
String access_token = getAccessToken();
// 通过调用接口获取考勤打卡结果
DingTalkClient clientDingTalkClient = new DefaultDingTalkClient(attendanceUrl);
OapiAttendanceListRequest requestAttendanceListRequest = new OapiAttendanceListRequest();
// 查询考勤打卡记录的起始工作日
requestAttendanceListRequest.setWorkDateFrom(from);
// 查询考勤打卡记录的结束工作日
requestAttendanceListRequest.setWorkDateTo(to);
// 员工在企业内的userid列表,最多不能超过50个。
requestAttendanceListRequest.setUserIdList(Arrays.asList(userId));
// 表示获取考勤数据的起始点
requestAttendanceListRequest.setOffset(0L);
// 表示获取考勤数据的条数,最大不能超过50条。
requestAttendanceListRequest.setLimit(50L);
OapiAttendanceListResponse response = null;
try {
response = clientDingTalkClient.execute(requestAttendanceListRequest, access_token);
} catch (ApiException e) {
e.printStackTrace();
}
return response;
}

public Boolean deleteEmp(String userId) {
// 获取access_token
String access_token = getAccessToken();
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/delete");
OapiUserDeleteRequest req = new OapiUserDeleteRequest();
req.setUserid(userId);
Boolean ret = false;
try {
OapiUserDeleteResponse rsp = client.execute(req, access_token);
if (rsp.getErrcode() == 0) {
ret = true;
}
} catch (ApiException e) {
e.printStackTrace();
}
return ret;
}

public HashMap<String, Object> deleteEmps(List<String> list) {
List<String> result = new ArrayList<>();
HashMap<String, Object> map = new HashMap<>();
if (list.size() > 0) {
for (String id : list) {
if (!deleteEmp(id)) {
result.add(id);
}
}
}
if (result.size() > 0) {
map.put("code", 1);
} else {
map.put("code", 0);
}
map.put("data", result);
return map;
}

public OapiAttendanceGetcolumnvalResponse.ColumnValListForTopVo getColumnval(String userId, String from, String to) {
// 获取access_token
String access_token = getAccessToken();
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getcolumnval");
OapiAttendanceGetcolumnvalRequest req = new OapiAttendanceGetcolumnvalRequest();
req.setUserid(userId);
// 7100724 上班1打卡时间 7100726 下班1打卡时间 7100728 上班2打卡时间 7100730 下班2打卡时间 7100732 上班3打卡时间 7100734 下班3打卡时间7100736 关联的审批单
req.setColumnIdList("7100724,7100726,7100728,7100730,7100732,7100734,7100736");
req.setFromDate(StringUtils.parseDateTime(from));
req.setToDate(StringUtils.parseDateTime(to));
OapiAttendanceGetcolumnvalResponse response = null;
OapiAttendanceGetcolumnvalResponse.ColumnValListForTopVo ret = null;
try {
response = client.execute(req, access_token);
if (response.isSuccess()) {
ret = response.getResult();
}
} catch (ApiException e) {
e.printStackTrace();
}
return ret;
}
/**
* 昨日某个员工打卡记录
* @return List
* 2022/03/08 11:00
*/
public List getRecordByUserId(String userId) {
String yestoday = DingdingUtil.getYestoday();
String from = yestoday + " 00:00:00";
String to = yestoday + " 23:59:59";
List<OapiAttendanceListResponse.Recordresult> list = new ArrayList();
OapiAttendanceListResponse attendanceListResponse = getAttendanceList(from, to, userId);
if (attendanceListResponse.isSuccess() && attendanceListResponse.getRecordresult().size() > 0) {
list = attendanceListResponse.getRecordresult();
}
return list;
}

/*时间函数*/
public static String getPastDate(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
String result = format.format(today);
return result;
}

/**
* 昨日日期
*
* @return
*/

public static String getYestoday() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
String yesterday = new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime());
return yesterday;
}

/**
* 获取上午下午
*
* @param seconds
* @return
*/
public static String timestamp2A(Long seconds) {
SimpleDateFormat sdf = new SimpleDateFormat("a");
return sdf.format(new Date(seconds));
}

/**
* 获取年
*
* @return
*/
public static String getYear() {
SimpleDateFormat format = new SimpleDateFormat("yyyy");
Date date = new Date();
return format.format(date);
}

/**
* 时间戳转换成时间格式字符串
*
* @param seconds
* @return
*/
public static String timestamp2Time(Long seconds) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf.format(new Date(seconds));
}

/**
* 时间戳转换成日期格式字符串
*
* @param strDate
* @return
*/
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}

public static String dateToHms(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}

/*时间函数*/
}


//"dingvvrsivpeg6bqm4qb";
//"wly_IoMnP0QXZzaWh-NiSOUxZJgpmDzZeKyy6LPQMktm9iO65RfO14LVYUliokL-";


// OapiDepartmentListResponse rsp = dingdingUtil.getDepartmentList();
// Map map = (Map) JSON.parse(rsp.getBody());
// Integer errcode= (Integer) map.get("errcode");
// List<DgdDingdingDayDept> deptList=new ArrayList<DgdDingdingDayDept>();
// if(errcode==0){
// List departments= (List) map.get("department");
// }

一辈子很短,努力的做好两件事就好;
第一件事是热爱生活,好好的去爱身边的人;
第二件事是努力学习,在工作中取得不一样的成绩,实现自己的价值,而不是仅仅为了赚钱;

继开 wechat
欢迎加我的微信,共同交流技术